package com.nanhai.buddhist.college.module.study.controller.admin.studyadditionalcourses;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.dto.ClassesBaseInfoDTO;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.dto.ClassesRespDTO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesClassDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.error;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;

import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studyadditionalcourses.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesDO;
import com.nanhai.buddhist.college.module.study.convert.studyadditionalcourses.StudyAdditionalCoursesConvert;
import com.nanhai.buddhist.college.module.study.service.studyadditionalcourses.StudyAdditionalCoursesService;

@Api(tags = "管理后台 - 选课管理")
@RestController
@RequestMapping("/study/additional-courses")
@Validated
public class StudyAdditionalCoursesController {

    @Resource
    private StudyAdditionalCoursesService additionalCoursesService;


    @Resource
    private CollegeClassApi collegeClassApi;


    @PostMapping("/create")
    @ApiOperation("创建选课管理")
    @PreAuthorize("@ss.hasPermission('study:additional-courses:create')")
    public CommonResult<String> createAdditionalCourses(@Valid @RequestBody StudyAdditionalCoursesCreateReqVO createReqVO) {
        return success(additionalCoursesService.createAdditionalCourses(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新选课管理")
    @PreAuthorize("@ss.hasPermission('study:additional-courses:update')")
    public CommonResult<Boolean> updateAdditionalCourses(@Valid @RequestBody StudyAdditionalCoursesUpdateReqVO updateReqVO) {
        additionalCoursesService.updateAdditionalCourses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除选课管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:additional-courses:delete')")
    public CommonResult<Boolean> deleteAdditionalCourses(@RequestParam("id") String id) {
        additionalCoursesService.deleteAdditionalCourses(id);
        return success(true);
    }


    @GetMapping("/get")
    @ApiOperation("获得选课管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:additional-courses:query')")
    public CommonResult<StudyAdditionalCoursesRespVO> getAdditionalCourses(@RequestParam("id") String id) {
        StudyAdditionalCoursesDO additionalCourses = additionalCoursesService.getAdditionalCourses(id);

        StudyAdditionalCoursesRespVO convert = StudyAdditionalCoursesConvert.INSTANCE.convert(additionalCourses);

        // 回显 班级信息
        Set<String> collegeClassIds = CollectionUtils.convertSet(additionalCourses.getList(), StudyAdditionalCoursesClassDO::getClassesId);
        if(!collegeClassIds.isEmpty()){
            CommonResult<List<ClassesRespDTO>> classesList = collegeClassApi.getClassesList(collegeClassIds);
            if(classesList.isError()){
                return error(classesList);
            }
            List<StudyAdditionalCoursesClassInfoVO> collect = classesList.getData().stream().
                    map(s -> StudyAdditionalCoursesClassInfoVO
                            .builder()
                            .id(s.getId())
                            .classesName(s.getClassesName())
                            .classesNumber(s.getClassesNumber())
                            .build())
                    .collect(Collectors.toList());
            convert.setClassInfoVOS(collect);
        }
        // 专业转集合
        if (StringUtils.isNotBlank(additionalCourses.getProfessionStr())){
             String[] split = additionalCourses.getProfessionStr().split(",");
            List<String> list = Arrays.asList(split);
            convert.setProfessionIds(list);
        }

        return success(convert);
    }

    @GetMapping("/list")
    @ApiOperation("获得选课管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('study:additional-courses:query')")
    public CommonResult<List<StudyAdditionalCoursesRespVO>> getAdditionalCoursesList(@RequestParam("ids") Collection<String> ids) {
        List<StudyAdditionalCoursesDO> list = additionalCoursesService.getAdditionalCoursesList(ids);
        return success(StudyAdditionalCoursesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得选课管理分页")
    @PreAuthorize("@ss.hasPermission('study:additional-courses:query')")
    public CommonResult<PageResult<StudyAdditionalCoursesRespVO>> getAdditionalCoursesPage(@Valid StudyAdditionalCoursesPageReqVO pageVO) {
        PageResult<StudyAdditionalCoursesDO> pageResult = additionalCoursesService.getAdditionalCoursesPage(pageVO);
        return success(StudyAdditionalCoursesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出选课管理 Excel")
    @PreAuthorize("@ss.hasPermission('study:additional-courses:export')")
    @OperateLog(type = EXPORT)
    public void exportAdditionalCoursesExcel(@Valid StudyAdditionalCoursesExportReqVO exportReqVO,
                                             HttpServletResponse response) throws IOException {
        List<StudyAdditionalCoursesDO> list = additionalCoursesService.getAdditionalCoursesList(exportReqVO);
        // 导出 Excel
        List<StudyAdditionalCoursesExcelVO> datas = StudyAdditionalCoursesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "选课管理.xls", "数据", StudyAdditionalCoursesExcelVO.class, datas);
    }


    @GetMapping("/getClassesListByProfessionIds")
    @ApiOperation("根据专业id集合获取班级列表")
    @ApiImplicitParam(name = "ids", value = "专业id", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ClassesBaseInfoDTO>> getClassesDTOListByProfessionIds(@RequestParam("ids") Collection<String> professionIds) {
        return collegeClassApi.selectClassBaseInfoByProfessionIds(professionIds);
    }

}
